/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#ifndef _MATH_H
#define _MATH_H

#include <stddef.h>
#include <float.h>

/**
 * Helper macro for C / C++ calles.
 */
#if defined __cplusplus
using namespace std;
#ifdef EXPORT_LOCAL
#undef EXPORT_LOCAL
#endif
#define EXPORT_LOCAL extern "C"
#else
#define EXPORT_LOCAL extern
#endif

/**
 * A function returns this value when the result of a mathematical operation yields a value
 * that is so large in magnitude that it is not representable with its return type. This is
 * one of the possible range errors, and is signaled by setting errno to ERANGE.<br>
 * Actually, functions can either return a positive or a negative HUGE_VAL
 * (HUGE_VAL or -HUGE_VAL) to at least indicate the sign of the result.
 */
#define HUGE_VAL DBL_MAX

#if defined __cplusplus
namespace std {
#endif

#if defined __cplusplus
	/**
	 * Breaks x into two parts: the integer part (stored in the object pointed by intpart)
	 * and the fractional part (returned by the function).
	 * Each part has the same sign as x.
	 *
	 * @param x Floating point value.
	 * @param buf Pointer to an object where the integral part is to be stored.
	 * @return The fractional part of x, with the same sign.
	 */
	double modf(double x, double *buf);
	/**
	 * Breaks x into two parts: the integer part (stored in the object pointed by intpart)
	 * and the fractional part (returned by the function).
	 * Each part has the same sign as x.
	 *
	 * @param x Floating point value.
	 * @param buf Pointer to an object where the integral part is to be stored.
	 * @return The fractional part of x, with the same sign.
	 */
	long double modf(long double x, long double *buf);
	/**
	 * Breaks x into two parts: the integer part (stored in the object pointed by intpart)
	 * and the fractional part (returned by the function).
	 * Each part has the same sign as x.
	 *
	 * @param x Floating point value.
	 * @param buf Pointer to an object where the integral part is to be stored.
	 * @return The fractional part of x, with the same sign.
	 */
	float modf(float x, float *buf);
	/**
	 * Returns the absolute value of x ( /x/ ).
	 * In C++, abs is also declared in this header with the same behavior.
	 *
	 * @param x Floating point value.
	 * @return The absolute value of x.
	 */
	double fabs(double x);
	/**
	 * Returns the absolute value of x ( /x/ ).
	 * In C++, abs is also declared in this header with the same behavior.
	 *
	 * @param x Floating point value.
	 * @return The absolute value of x.
	 */
	long double fabs(long double x);
	/**
	 * Returns the absolute value of x ( /x/ ).
	 * In C++, abs is also declared in this header with the same behavior.
	 *
	 * @param x Floating point value.
	 * @return The absolute value of x.
	 */
	float fabs(float x);
#else
	/**
	 * Breaks x into two parts: the integer part (stored in the object pointed by intpart)
	 * and the fractional part (returned by the function).
	 * Each part has the same sign as x.
	 *
	 * @param x Floating point value.
	 * @param buf Pointer to an object where the integral part is to be stored.
	 * @return The fractional part of x, with the same sign.
	 */
	EXPORT_LOCAL double modf(double x, double *buf);
	/**
	 * Returns the absolute value of x ( /x/ ).
	 * In C++, abs is also declared in this header with the same behavior.
	 *
	 * @param x Floating point value.
	 * @return The absolute value of x.
	 */
	EXPORT_LOCAL double fabs(double x);
#endif

#if defined __cplusplus
}
#endif

#endif
